<?php
namespace Common\Models;
use Common\BaseModel;
use Common\Common;
use Library\Validators\INArray;
use Services\Api\GoodsApiServices;

class Footprints extends BaseModel
{
	public $res;
	public $redis_key="";
    public function initialize()
    {
        parent::initialize();
        $this->setSource('f_footprints');
    }

	public function  find_first($data){
	    $conditions = "product_id='{$data['product_id']}' and site='{$data['site']}' and user_id={$data['user_id']}";
        $product_collect = $this->findFirst(array(
            $conditions
        ));
        if(empty($product_collect)){
            return false;
        }else{
            return $product_collect;
        }
    }

	/**
     * @func 添加商品 最多100条足迹
     * @param $user_id 用户id
     * @param $params 商品id和站点
     */
	public function add_footprints($params){
        if(!isset($params['goods_id']) || empty($params['goods_id'])){
            return ['status'=>false,'msg'=>'商品id不能为空'];
        }
        $data['product_id'] = $params['goods_id'];
        if(!isset($params['site']) || empty($params['site'])){
            return ['status'=>false,'msg'=>'站点不能为空'];
        }else{
            if (!INArray::platformSite($params['site'])) {
                return ['status'=>false,'msg'=>'站点错误'];
            }
        }
        if($this->search_current_footprints($params['user_id'],$params['site'],$params['goods_id'])){
            return ['status'=>false,'msg'=>'已经添加足迹'];
        }
        $footprintsDb = new Footprints();
        $GoodsServices = new GoodsApiServices();
        $goods_detail = $GoodsServices->goods_detail($params['site'],$params['goods_id']);
        if(!$goods_detail['status']){
            $this->fail(-1,$goods_detail['msg']);
        }
        $res = $goods_detail['data'];
        if($params['site'] == 'self'){
            $BShopGoodsDb = new BShopGoods();
            $res = $BShopGoodsDb->get_goods_sn($params);
        }
        $footprintsDb->user_id = $params['user_id'];
        $footprintsDb->site = $params['site'];
        $footprintsDb->product_id = $res['GoodsId'];
        $footprintsDb->ShopType = $res['ShopType'];
        $footprintsDb->goodsTitle = $res['ShortTitle'];
        $footprintsDb->ShopName = $res['ShopName'];
        $footprintsDb->ShortTitle = $res['ShortTitle'];
        $footprintsDb->Price = $res['Price'];
        $footprintsDb->PicLogo = $res['PicLogo'];
        $footprintsDb->CouponMoney = $res['CouponMoney'];
        $footprintsDb->MonthSale = $res['MonthSale'];
        $footprintsDb->EndPrice = $res['EndPrice'];
        $footprintsDb->CouponUrl = isset($res['Couponurl']) ? $res['Couponurl'] : '';
        $footprintsDb->Commission = Common::bc($res['Commission'],'0','+');
        $footprintsDb->MaxCommission = Common::bc($res['Commission'],'0','+');
        $footprintsDb->created_at = date('Y-m-d H:i:s');
        $footprintsDb->save();
        $this->delete_footprint($params['user_id']);
    }

    /**
     * @func 删除
     *
     */
    public function del_footprint($ids){
        $ret= $this->find('id IN ('.$ids.')');
        return $ret->delete();
    }


    /**
     * @param $user_id 清空足迹
     * @return mixed
     */
    public function cleardel_footprint($user_id){
        $ret= $this->find("user_id=$user_id");
        return $ret->delete();
    }



    public function getList($data){
        $page = intval($data['page']);
        $size = intval($data['page_size']);
        if ($page > 0) {
            $page = ($page - 1) * $size;
        }
        $condetions="user_id={$data['user_id']}";
        $listObj = $this->find(array(
                $condetions,
                'order'=>'id desc',
                'limit'   => $size,
                'offset'  => $page
            )
        )->toarray();
        foreach($listObj as $key=>$value){
            $listObj[$key]['Site'] = $value['site'];
            $listObj[$key]['GoodsId'] = $value['product_id'];
        }
        return $listObj;
    }



    /**
     * @func 查询足迹总数
     */
    public function getfootPrintsCount($user_id){
        $conditions = "user_id = ?1";
        $parameters = array(1 => $user_id);
        $count_array = $this :: count(array(
            $conditions,
            "column" => "id",
            "bind" => $parameters
        ));
        return $count_array;
    }



    /**
     * 删除足迹大于50的记录
     */
    public function delete_footprint($user_id,$limit=50){
        $conditions="user_id={$user_id}";
        $count = $this->count(array(
            $conditions,
        ));
        if($count>$limit){
            $new_conditions="user_id={$user_id}";
            $res = $this->find(array(
                $new_conditions,
                'order'=>'id asc',
            ))->delete();
            if($res){
                return ['status'=>true,'msg'=>'删除成功'];
            }else{
                return ['status'=>false,'msg'=>'删除失败'];
            }
        }
    }


    /**
     * 查询当前的足迹是否已经添加
     */
    public function search_current_footprints($user_id,$site,$goods_id){
        $conditions = "user_id={$user_id} AND site='{$site}' AND product_id='{$goods_id}'";
        $is_exits = $this->findFirst(array(
            $conditions,
            'order'=>'id desc'
        ));
        if($is_exits){
            return true;
        }else{
            return false;
        }
    }



}
